//
//  WatchTopVC.m
//  DayDay
//
//  Created by wade on 17/2/13.
//  Copyright © 2017年 wade. All rights reserved.
//

#import "WatchTopVC.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "BaiduAdView.h"
@interface WatchTopVC ()

@end

@implementation WatchTopVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self getData:self.date];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)getData:(NSString *)date
{
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setTimeoutInterval:30];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    
    [manager GET:[NSString stringWithFormat:@"http://www.6hck.co/%@",date] parameters:nil
        progress:^(NSProgress * _Nonnull downloadProgress)
     {
         //NSLog(@"%lld", downloadProgress.totalUnitCount);
     }
         success:^(NSURLSessionDataTask *operation, id responseObject)
     {
         
         NSString *str = [[NSString alloc] initWithData:responseObject  encoding:NSUTF8StringEncoding];
         
         NSRange range;
         range = [str rangeOfString:@"查看原图"];
         if (range.location != NSNotFound) {
             NSLog(@"found at location = %lu",(unsigned long)range.location);
             
             
             NSRange tsRange = [str rangeOfString:@"class=cur>第"];
             NSString *ts = [str substringWithRange:NSMakeRange(tsRange.location+10, 5)];
             NSLog(@"%@",ts);
             
             
             
             NSString *ss = [str substringWithRange:NSMakeRange(range.location-108,100)];
             
             NSRange ranges = [ss rangeOfString:@"<a href="]; //现获取要截取的字符串位置
             NSString * result = [ss substringFromIndex:ranges.location+9]; //截取字符串
             
             
             NSRange rangess = [result rangeOfString:@".jpg"]; //现获取要截取的字符串位置
             NSString * results = [result substringToIndex:rangess.location+4];
             
             NSLog(@"%@",results);
             
             //[self.imgView sd_setImageWithURL:[NSURL URLWithString:results]];
             
             [self.imgView sd_setImageWithURL:[NSURL URLWithString:results] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                 
                 self.titleLab.text = ts;
                 
             }];
             
             
         }else{
             NSLog(@"Not Found");
         }
         
         
     }
         failure:^(NSURLSessionDataTask *operation, NSError *error)
     {
         NSLog(@"%@",error);
         self.titleLab.text = @"获取不到了～～骚等在刷...";
     }];
    
    
    
}
@end
